class Solution
{
public:
    int countDigitOne(int n)
    {
        int count = 0;
        for (long long i = 1; i <= n; i *= 10)
        {
            long long t = i * 10;
            count += (n / t) * i + min(max(n % t - i + 1, 0LL), i);
        }
        return count;
    }
};